package learning.spring.helloworld;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 实现bean后置处理器接口 BeanPostProcessor，在Spring容器初始化“所有”bean之前和之后对bean进行拦截处理
 */
public class HelloBeanPostProcessor implements BeanPostProcessor {

    /**
     * 在"所有"bean初始化之前调用
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // System.out.println("Hello postProcessBeforeInitialization");

        if ("hello".equals(beanName)) {// 可以根据bean名称来过滤
            System.out.println("Hello postProcessBeforeInitialization");
        }

        return bean;
    }

    /**
     * 在"所有"bean初始化之后调用
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // System.out.println("Hello postProcessAfterInitialization");

        if ("hello".equals(beanName)) {
            System.out.println("Hello postProcessAfterInitialization");
        }

        return bean;
    }

}
